package com.example.mywanandroid.ui.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.databinding.library.baseAdapters.BR;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


import com.example.mywanandroid.R;
import com.example.mywanandroid.bean.responsebean.ArticleBean;
import com.example.mywanandroid.bean.responsebean.home.HomeData;
import com.example.mywanandroid.config.Constants;
import com.example.mywanandroid.databinding.ItemArticleBinding;
import com.example.mywanandroid.databinding.ItemHomeBannerBinding;
import com.example.mywanandroid.databinding.ItemHomeTopBinding;
import com.example.mywanandroid.ui.activity.login.LoginActivity;
import com.orhanobut.hawk.Hawk;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.List;

import static com.example.mywanandroid.config.App.getContext;


/**
 * 主页的Adapter
 */
public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "HomeAdapter";
    private Context mContext;

    private List<HomeData> mList;
    private OnHomeClickListener mOnHomeClickListener;
    private OnTopArticleClickListener mOnTopArticleClickListener;

    public HomeAdapter() {
    }

    public HomeAdapter(Context context) {
        mContext = context;
    }


    /**
     * 添加监听回调
     *
     * @param itemData
     */
    public void addTopClickListener(ArticleBean itemData) {
    }

    /**
     * 添加监听回调
     *
     * @param itemData
     */
    public void addTopCollectListener(ArticleBean itemData) {
    }


    /**
     * 改变数据
     *
     * @param newItemDatas
     */
    public void onItemDatasChanged(List<HomeData> newItemDatas) {
        this.mList = newItemDatas;
        notifyDataSetChanged();
    }

    public void cleanData() {
        this.mList.clear();
        notifyDataSetChanged();
    }

    public List<HomeData> getHomeData() {
        return mList;
    }

    public int getItemLayout(HomeData itemData) {
        if (itemData.getBannerData() != null) {
            return R.layout.item_home_banner;
        } else if (itemData.getTopArticleList() != null) {
            return R.layout.item_home_top;
        } else {
            return R.layout.item_article;
        }
    }


    @Override
    public int getItemViewType(int position) {
        return getItemLayout(mList.get(position));
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), viewType, viewGroup, false);
        return new CommonViewHolder(dataBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        HomeData homeData = mList.get(position);
        Log.d(TAG, "onBindViewHolder: ");
        CommonViewHolder commonViewHolder = (CommonViewHolder) viewHolder;
        if (homeData.getArticleList() != null) {
            //绑定数据
            ItemArticleBinding binding = (ItemArticleBinding) commonViewHolder.binding;
            binding.setArticleBean(homeData.getArticleList());
            commonViewHolder.setOnClickListener(R.id.card_view, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnHomeClickListener != null) {
                        mOnHomeClickListener.toDetailsActivity(mList.get(position));
                    }
                }
            });
            binding.ivCollect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Hawk.get(Constants.HawkCode.LOGIN_DATA) != null) {
                        homeData.getArticleList().setCollect(! homeData.getArticleList().isCollect());
                        notifyDataSetChanged();
                        if (mOnHomeClickListener != null) {
                            mOnHomeClickListener.onArticleCollectClick(mList.get(position).getArticleList());
                        }
                    }
                }
            });
        } else if (homeData.getTopArticleList() != null) {
            ItemHomeTopBinding binding = (ItemHomeTopBinding) commonViewHolder.binding;
            binding.setTopArticle(homeData.getTopArticleList());
            showTopArticle(binding.recyclerView, homeData.getTopArticleList(), position);
        } else {
            ItemHomeBannerBinding binding = (ItemHomeBannerBinding) commonViewHolder.binding;
            binding.banner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
                @Override
                public void onPageClick(View view, int i) {
                    if (mOnHomeClickListener != null) {
                        mOnHomeClickListener.toDetailsActivity(mList.get(position), i);
                    }
                }
            });
            binding.setBannerData(homeData.getBannerData());
        }
        //防止数据闪烁
        ((CommonViewHolder) viewHolder).binding.executePendingBindings();
    }

    private void showTopArticle(RecyclerView recyclerView, HomeData.TopArticle topArticle, int fatherPosition) {
        CommonAdapter commonAdapter = new CommonAdapter<ArticleBean>(topArticle.getArticleBeanList(), R.layout.item_article, BR.articleBean) {

            @Override
            public void addListener(View root, ArticleBean itemData, int position) {
                root.findViewById(R.id.card_view).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnTopArticleClickListener != null) {
                            mOnTopArticleClickListener.onTopClick(itemData);
                        }
                    }
                });

                root.findViewById(R.id.iv_collect).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (Hawk.get(Constants.HawkCode.LOGIN_DATA) != null) {
                            itemData.setCollect(!itemData.isCollect());
                            notifyDataSetChanged();
                            if (mOnTopArticleClickListener != null) {
                                mOnTopArticleClickListener.onTopCollectClick(itemData);
                            }
                        } else {
                            LoginActivity.start(mContext);
                        }
                    }
                });
            }
        };
        recyclerView.setAdapter(commonAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }

    public interface OnHomeClickListener {

        void toDetailsActivity(HomeData itemData);

        void toDetailsActivity(HomeData itemData, int position);

        void onArticleCollectClick(ArticleBean articleList);
    }

    public void setOnHomeDataClickListener(OnHomeClickListener listener) {
        this.mOnHomeClickListener = listener;
    }

    public interface OnTopArticleClickListener {
        void onTopCollectClick(ArticleBean articleBean);

        void onTopClick(ArticleBean articleBean);
    }

    public void setOnTopArticleClickListener(OnTopArticleClickListener listener) {
        this.mOnTopArticleClickListener = listener;
    }
}
